import {http} from '@/plugins/net.js'


/**
 * 登录
 */
export const login = async (param, router) => {
    const res = await http(`/login`, 'post', param);
    if (res) {
        // 存储token到sessionStorage
        sessionStorage.setItem('token', res)
        // 获取用户信息
        getLoginUserInfo().then(success => {
            // 页面跳转
            let path = router.currentRoute.query.redirect
            // router.replace((path === '/' || path === undefined) ? '/home' : path)
            router.replace('/home')
        })

    }
}

/**
 * 获取登录用户详细信息
 * @returns {Promise<void>}
 */
export const getLoginUserInfo = async () => {
    const res = await http('/userinfo', 'get', null);
    if (res) {
        // 存放到本地会话中
        sessionStorage.setItem('userinfo', JSON.stringify(res))
    }
}

/**
 * 获取登录用户的导航菜单
 * @returns {Promise<*>}
 */
export function getMenuTree() {
    return http('/menu/tree', 'get', null).then(success => {
        if (success) {
            return success
        }
    })
}
